package com.anXin.domestic.controller;

import com.anXin.domestic.service.DomesticOrdersService;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.service.dtos.DomesticOrdersDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 跑腿订单
 * @author 鹏
 */
@Slf4j
@RestController
@RequestMapping("/domestic/orders")
public class DomesticOrdersController {

    @Autowired
    private DomesticOrdersService domesticOrdersService;

    /**
     * 新增订单
     * @return
     */
    @PostMapping("/add")
    public ResponseResult addOrders(@RequestBody DomesticOrdersDTO domesticOrdersDTO) {
        log.info("接收到前端发送的数据: {}",domesticOrdersDTO);
        return domesticOrdersService.addOrders(domesticOrdersDTO);
    }

    /**
     * 根据订单id查看订单
     */
    @GetMapping("/get")
    public ResponseResult getOrders(@RequestParam("id") Long orderId) {
        return domesticOrdersService.getOrders(orderId);
    }

    /**
     * 客户查看自己所有的订单
     * @return
     */
    @GetMapping("/get/client")
    public ResponseResult getClientOrders() {
        return domesticOrdersService.getClientOrders();
    }

    /**
     * 服务人员查看自己的接单
     * @return
     */
    @GetMapping("/get/server")
    public ResponseResult getServerOrders() {
        return domesticOrdersService.getServerOrders();
    }

    /**
     * 取消订单
     * @param postId
     * @return
     */
    @PostMapping("/cancel")
    public ResponseResult cancelOrders(@RequestParam("id") Long postId) {
        return domesticOrdersService.cancelOrders(postId);
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @PostMapping("/delete")
    public ResponseResult deleteOrders(@RequestParam("id") Long orderId) {
        if (domesticOrdersService.removeById(orderId)) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS.getCode(),"删除订单成功!");
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.OPERATE_BUSY_ERROR.getCode(),"服务器繁忙,删除订单失败!请稍后重试");
        }
    }

}
